/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.datatype;

import static org.sss.mapreduce.datatype.MsgPack.instance;

import java.util.Arrays;

/**
 * A Packable class that encapsulates a String object.
 */
public class DCPackableString implements SelfPackable {
  private static final String EMPTY_STRING = new String();
  private byte hashCode;
  private byte slot;
  private String string;
  /**
   * Constructs a newly allocated DCPackableString object that it represents
   * an empty String.
   */
  public DCPackableString() { this(EMPTY_STRING, 0, 0); }
  /**
   * Constructs a newly allocated DCPackableString object that represents
   * the primitive String argument.
   * @param original the value to be represented by the DCPackableString.
   */
  public DCPackableString(String original, int hashCode, int slot) {
    if (original == null) { original = EMPTY_STRING; }
    this.string = original;
    this.hashCode = (byte) hashCode;
    this.slot = (byte) slot;
  }
  /**
   * Return the String value of this DCPackableString object.
   * @return the String value represented by this object
   */
  public String get() { return this.string; }
  /**
   * Indicates whether some other object is "equal to" this one.
   * see {@link Packable#equals}
   */
  @Override
  public boolean equals(Object o) {
    if (o instanceof DCPackableString) {
      if (o == this) {
        return true;
      }
      DCPackableString o_ = (DCPackableString) o;
      return string.equals(o_.get()) && hashCode == o_.hashCode() &&
        slot == o_.slot();
    }
    else {
      return false;
    }
  }
  /**
   * Returns a hash code value for the object.
   * see {@link Packable#hashCode}
   */
  @Override
  public int hashCode() {
    return hashCode;
  }
  /**
   * Indicates which slot this object should be put.
   * see {@link Packable#slot}
   */
  @Override
  public int slot() {
    return slot;
  }
  /**
   * Deserialize this object from the specified raw byte array.
   * see {@link SelfPackable#loadBytes}
   */
  @Override
  public DCPackableString loadBytes(byte[] bytes) throws Exception {
    this.hashCode = bytes[0];
    this.slot = bytes[1];
    byte[] bytes_ = Arrays.copyOfRange(bytes, 2, bytes.length);
    this.string = instance.read(bytes_, String.class);
    return this;
  }
  /**
   * Serialize this object into a sequence of bytes.
   * see {@link SelfPackable#toBytes}
   */
  @Override
  public byte[] toBytes() throws Exception {
    byte[] body = instance.write(this.string);
    byte[] tmp = new byte[2 + body.length];
    tmp[0] = hashCode;
    tmp[1] = slot;
    for (int i=0; i<body.length; ++i) {
      tmp[2 + i] = body[i];
    }
    return tmp;
  }

  @Override
  public long getRoughSize() {
    return 48 + 2 + string.length() * 2;
  }
}
